<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    .grid {
      margin: auto;
      width: 530px;
      text-align: center;
    }

    .grid table {
      width: 100%;
      border-collapse: collapse;
    }

    .grid th,
    td {
      padding: 10;
      border: 1px dashed orange;
      height: 35px;
      line-height: 35px;
    }

    .grid th {
      background-color: orange;
    }

    .grid .book {
      padding-bottom: 10px;
      padding-top: 5px;
      background-color: #F3DCAB;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="grid">
      <div>
        <h1>图书管理</h1>
        <div class="book">
          <div>
            <label for="id">
              编号：
            </label>
            <input type="text" id="id" disabled>
            <label for="name">
              名称：
            </label>
            <input type="text" id="name" v-model='name'>
            <button @click='handle' :disabled='subFlag'>提交</button>
          </div>
        </div>
        <div class="count">总数: <span>{{totalBooks}}</span></div>
      </div>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr :key='item.id' v-for='item in books'>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date|format}}</td>
            <td>
              <a href="" @click.prevent='edit(item.id)'>修改</a>
              <span>|</span>
              <a href="" @click.prevent='del(item.id)'>删除</a>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <script type="text/javascript" src="js/vue.js"></script>
  <script src="./js/axios.js"></script>
  <script type="text/javascript">
    axios.interceptors.request.use(config => {
      config.baseURL = 'http://127.0.0.1:3000'
      return config
    })
    axios.interceptors.response.use(res => {
      return res.data
    })
    var vm = new Vue({
      el: '#app',
      data: {
        subFlag: false,
        editId: '',
        flag: true,
        name: '',
        count: '',
        books: []
      },
      methods: {
        handle() {
          if (this.name === '') return
          //添加 提交
          if (this.flag) {
            let me = this
            async function add() {
              await axios.post('books', { name: me.name })
              me.queryBooks()
            }
            add()
          } else {
            // 编辑提交
            this.editBooks()
            this.queryBooks()
            this.flag = true
          }
          this.name = ''
        }
        //删除
        , async del(v) {
          let res = await axios.delete('books/' + v)
          this.queryBooks()
        },
        // 编辑
        async edit(v) {
          this.flag = false
          //信息展示
          let data = await axios.get('books/' + v)
          this.name = data.name
          this.editId = data.id
        },
        queryBooks() {
          // async返回的是promise对象
          axios.get('books').then(data => {
            this.books = data
          })
        },
        editBooks() {
          axios.put('books/' + this.editId, { name: this.name, date: +new Date() })
        }

      },
      computed: {
        totalBooks() {
          return this.books.length
        }
      },
      filters: {
        format(v) {
          return new Date(v).toLocaleString()
        }
      },
      watch: {
        async name(n) {
          let res = await axios.get('books/book/' + n)
          if (res.status == 1) {
            this.subFlag = true
          } else {
            this.subFlag = false
          }
        }
        //监听对象里面的属性
        //'form.name'(n){}
      },
      mounted() {
        // 该生命周期钩子函数被触发的时候，模板已经可以使用
        // 一般此时用于获取后台数据，然后把数据填充到模板
        this.queryBooks()
      },
    });
  </script>
</body>

</html>